//
//  DINGView.m
//  商城
//
//  Created by runone02 on 2018/11/6.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "DINGView.h"
#import "LTSCalendarManager.h"


#define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define RandColor RGBColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))
@interface DINGView()<LTSCalendarEventSource>{
    NSMutableDictionary *eventsByDate;
}
@property (nonatomic,strong)LTSCalendarManager *manager;
@end
@implementation DINGView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {

    self.backTadayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backTadayButton setTitle:@"回到今天" forState:UIControlStateNormal];
    [self.backTadayButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.backTadayButton.titleLabel.font = TITLE_FONT(30);
    [self.backTadayButton addTarget:self action:@selector(backTadayButtonClick) forControlEvents:UIControlEventTouchDragInside];
    [self addSubview:self.backTadayButton];
    [self.backTadayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).with.offset(W_RATIO(30));
        make.top.mas_equalTo(self).with.offset(W_RATIO(30));
    }];
    
    self.lunarCalendarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.lunarCalendarButton setTitle:@"显示农历" forState:UIControlStateNormal];
    [self.lunarCalendarButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.lunarCalendarButton.titleLabel.font = TITLE_FONT(30);
    [self.lunarCalendarButton addTarget:self action:@selector(lunarCalendarButtonClick) forControlEvents:UIControlEventTouchDragInside];
    [self addSubview:self.lunarCalendarButton];
    [self.lunarCalendarButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).with.offset(W_RATIO(30));
        make.centerX.mas_equalTo(self);
    }];
    
    self.changeColorButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.changeColorButton setTitle:@"改变背景颜色" forState:UIControlStateNormal];
    [self.changeColorButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.changeColorButton.titleLabel.font = TITLE_FONT(30);
    [self.changeColorButton addTarget:self action:@selector(changeColorButtonClick) forControlEvents:UIControlEventTouchDragInside];
    [self addSubview:self.changeColorButton];
    [self.changeColorButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).with.offset(W_RATIO(30));
        make.right.mas_equalTo(self).with.offset(-W_RATIO(30));
    }];
    
    
    self.lastMonthButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.lastMonthButton setTitle:@"上个月" forState:UIControlStateNormal];
    [self.lastMonthButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.lastMonthButton.titleLabel.font = TITLE_FONT(30);
    [self.lastMonthButton addTarget:self action:@selector(lastMonthButtonClick) forControlEvents:UIControlEventTouchDragInside];
    [self addSubview:self.lastMonthButton];
    [self.lastMonthButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
     make.top.mas_equalTo(self.backTadayButton.mas_bottom).with.offset(W_RATIO(10));
        make.left.mas_equalTo(self).with.offset(W_RATIO(30));
    }];
    
    self.currentDateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.currentDateButton setTitle:@"当前日期" forState:UIControlStateNormal];
    [self.currentDateButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.currentDateButton.titleLabel.font = TITLE_FONT(30);

    [self addSubview:self.currentDateButton];
    [self.currentDateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.backTadayButton.mas_bottom).with.offset(W_RATIO(10));
        make.centerX.mas_equalTo(self);
    }];

    self.nextMonthButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.nextMonthButton setTitle:@"下个月" forState:UIControlStateNormal];
    [self.nextMonthButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.nextMonthButton.titleLabel.font = TITLE_FONT(30);
    [self.nextMonthButton addTarget:self action:@selector(nextMonthButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.nextMonthButton];
    [self.nextMonthButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.backTadayButton.mas_bottom).with.offset(W_RATIO(10));
        make.right.mas_equalTo(self).with.offset(-W_RATIO(30));
    }];

    self.MondayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.MondayButton setTitle:@"周一开始" forState:UIControlStateNormal];
    [self.MondayButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.MondayButton.titleLabel.font = TITLE_FONT(30);
    [self.MondayButton addTarget:self action:@selector(MondayButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.MondayButton];
    [self.MondayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.lastMonthButton.mas_bottom).with.offset(W_RATIO(10));
        make.left.mas_equalTo(self).with.offset(W_RATIO(30));
    }];
    
    self.SundayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.SundayButton setTitle:@"周日开始" forState:UIControlStateNormal];
    [self.SundayButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.SundayButton.titleLabel.font = TITLE_FONT(30);
    [self.SundayButton addTarget:self action:@selector(SundayButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.SundayButton];
    [self.SundayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.lastMonthButton.mas_bottom).with.offset(W_RATIO(10));
        make.right.mas_equalTo(self).with.offset(-W_RATIO(30));
    }];

    
    self.sinWeekButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sinWeekButton setTitle:@"收起日历" forState:UIControlStateNormal];
    [self.sinWeekButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.sinWeekButton.titleLabel.font = TITLE_FONT(30);
    [self.sinWeekButton addTarget:self action:@selector(sinWeekButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.sinWeekButton];
    [self.sinWeekButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.MondayButton.mas_bottom).with.offset(W_RATIO(10));
        make.left.mas_equalTo(self).with.offset(W_RATIO(30));
    }];
    
    self.allWeekButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.allWeekButton setTitle:@"弹出日历" forState:UIControlStateNormal];
    [self.allWeekButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.allWeekButton.titleLabel.font = TITLE_FONT(30);
    [self.allWeekButton addTarget:self action:@selector(allWeekButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.allWeekButton];
    [self.allWeekButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.MondayButton.mas_bottom).with.offset(W_RATIO(10));
        make.right.mas_equalTo(self).with.offset(-W_RATIO(30));
    }];

    self.xingqiButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.xingqiButton setTitle:@"星期一" forState:UIControlStateNormal];
    [self.xingqiButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.xingqiButton.titleLabel.font = TITLE_FONT(30);
    [self.xingqiButton addTarget:self action:@selector(xingqiButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.xingqiButton];
    [self.xingqiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.sinWeekButton.mas_bottom).with.offset(W_RATIO(10));
        make.left.mas_equalTo(self).with.offset(W_RATIO(30));
    }];
    
    self.zhouyiButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.zhouyiButton setTitle:@"周一" forState:UIControlStateNormal];
    [self.zhouyiButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.zhouyiButton.titleLabel.font = TITLE_FONT(30);
    [self.zhouyiButton addTarget:self action:@selector(zhouyiButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.zhouyiButton];
    [self.zhouyiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.sinWeekButton.mas_bottom).with.offset(W_RATIO(10));
        make.centerX.mas_equalTo(self);
    }];
    
    self.yiButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.yiButton setTitle:@"一" forState:UIControlStateNormal];
    [self.yiButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.yiButton.titleLabel.font = TITLE_FONT(30);
    [self.yiButton addTarget:self action:@selector(yiButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.yiButton];
    [self.yiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.sinWeekButton.mas_bottom).with.offset(W_RATIO(10));
        make.right.mas_equalTo(self).with.offset(-W_RATIO(30));
    }];
    
    self.manager = [LTSCalendarManager new];
    self.manager.eventSource = self;
    self.manager.weekDayView = [[LTSCalendarWeekDayView alloc]init];
    [self addSubview:self.manager.weekDayView];
    
    [self.manager.weekDayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.xingqiButton.mas_bottom).with.offset(W_RATIO(30));
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(30);
    }];
    self.manager.calenderScrollView = [[LTSCalendarScrollView alloc] init];
    [self addSubview:self.manager.calenderScrollView];
    [self.manager.calenderScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.manager.weekDayView.mas_bottom);
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self).with.offset(-30);     
    }];
    [self createRandomEvents];
//    self.automaticallyAdjustsScrollViewInsets = false;

}

- (void)createRandomEvents{
    eventsByDate = [NSMutableDictionary new];
    
    for(int i = 0; i < 30; ++i){
        // Generate 30 random dates between now and 60 days later
        NSDate *randomDate = [NSDate dateWithTimeInterval:(rand() % (3600 * 24 * 60)) sinceDate:[NSDate date]];

        NSString *key = [[self dateFormatter] stringFromDate:randomDate];
        if(!eventsByDate[key]){
            eventsByDate[key] = [NSMutableArray new];
        }
        
        [eventsByDate[key] addObject:randomDate];
    }
    [self.manager reloadAppearanceAndData];
}

- (NSDateFormatter *)dateFormatter{
    static NSDateFormatter *dateFormatter;
    if(!dateFormatter){
        dateFormatter = [NSDateFormatter new];
        dateFormatter.dateFormat = @"yyyy.MM.dd";
    }
    return dateFormatter;
}


//当前 选中的日期  执行的方法
- (void)calendarDidSelectedDate:(NSDate *)date {
    
    NSString *key = [[self dateFormatter] stringFromDate:date];
    
    [self.currentDateButton setTitle:key forState:UIControlStateNormal];
    NSArray *events = eventsByDate[key];
//    self.title = key;
    NSLog(@"%@",date);
    if (events.count>0) {
        //该日期有事件    tableView 加载数据
    }
}

// 该日期是否有事件
- (BOOL)calendarHaveEventWithDate:(NSDate *)date {
    
    NSString *key = [[self dateFormatter] stringFromDate:date];
    
    if(eventsByDate[key] && [eventsByDate[key] count] > 0){
        return YES;
    }
    return NO;
}

/**
 回到今天
 */
- (void)backTadayButtonClick{
     [self.manager goBackToday];
}


/**
 显示农历
 */
- (void)lunarCalendarButtonClick{
    [LTSCalendarAppearance share].isShowLunarCalender = ![LTSCalendarAppearance share].isShowLunarCalender;
    //重新加载外观
    [self.manager reloadAppearanceAndData];
}


/**
 改变背景颜色
 */
- (void)changeColorButtonClick{
    [LTSCalendarAppearance share].calendarBgColor = RandColor;
    [LTSCalendarAppearance share].weekDayBgColor = RandColor;
    [LTSCalendarAppearance share].dayCircleColorSelected = RandColor;
    [LTSCalendarAppearance share].dayCircleColorToday = RandColor;
    [LTSCalendarAppearance share].dayBorderColorToday = RandColor;
    [LTSCalendarAppearance share].dayDotColor = RandColor;
    [LTSCalendarAppearance share].dayDotColor = RandColor;
    [LTSCalendarAppearance share].lunarDayTextColor = RandColor;
    
    [self.manager reloadAppearanceAndData];
}


/**
 上个月
 */
- (void)lastMonthButtonClick{
     [self.manager loadPreviousPage];
}


/**
 下个月
 */
- (void)nextMonthButtonClick{
     [self.manager loadNextPage];
}


/**
 周一开始
 */
- (void)MondayButtonClick{
    [LTSCalendarAppearance share].firstWeekday = 2;
    [self.manager reloadAppearanceAndData];
}


/**
 周日开始
 */
- (void)SundayButtonClick{
    [LTSCalendarAppearance share].firstWeekday = 1;
    
    [self.manager reloadAppearanceAndData];
}


/**
 收起日历
 */
- (void)sinWeekButtonClick{
   [self.manager showSingleWeek];
}


/**
 弹出日历
 */
- (void)allWeekButtonClick{
     [self.manager showAllWeek];
}


/**
 星期一
 */
- (void)xingqiButtonClick{
    [LTSCalendarAppearance share].weekDayFormat = LTSCalendarWeekDayFormatFull;
    [self.manager.weekDayView reloadAppearance];
}


/**
 周一
 */
- (void)zhouyiButtonClick{
    [LTSCalendarAppearance share].weekDayFormat = LTSCalendarWeekDayFormatShort;
    [self.manager.weekDayView reloadAppearance];
}


/**
 一
 */
- (void)yiButtonClick{
    [LTSCalendarAppearance share].weekDayFormat = LTSCalendarWeekDayFormatSingle;
    [self.manager.weekDayView reloadAppearance];
}
@end
